/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-11-03 13:55:26   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.cloud.domain.CollegeDepartment;
import com.zx.cloud.domain.CollegeLocation;
import com.zx.cloud.domain.CollegeMessage;
import com.zx.cloud.domain.CollegeName;
import com.zx.cloud.persistence.CollegeMessageMapper;
import com.zx.cloud.persistence.CrudMapper;

/**
 * 
 */
@Service
public class CollegeMessageService extends CrudServiceImpl<CollegeMessage>{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(CollegeMessageService.class);
    
    @Autowired
    private CollegeMessageMapper collegeMessageMapper;
    
  	@Autowired
    public CollegeMessageService(CrudMapper<CollegeMessage> mapper) {
        super(mapper);
    }

  	public List<CollegeDepartment> findGlobalMsg(){
  		
  		List<CollegeDepartment> resultList = new ArrayList<CollegeDepartment>();
  		
  		List<CollegeMessage> departments = collegeMessageMapper.findDepartment();
  		
  		if(null != departments && departments.size() > 0){
  			
  			int countFlag = 1;
  			for(CollegeMessage msg : departments){
  				CollegeDepartment department = new CollegeDepartment();
				department.setDepartId(countFlag+"");
				department.setDepartName(msg.getDepartment());
				resultList.add(department);
				countFlag++;
  			}
  		}
  		
  		List<CollegeMessage> locations = collegeMessageMapper.findLocation();
  		
  		if(null != resultList && resultList.size() > 0){
  			
  			for(CollegeDepartment msg : resultList){
  			
  				String departId = msg.getDepartId();
  				String departName = msg.getDepartName();
  				
  				List<CollegeLocation> location = new ArrayList<CollegeLocation>();
  				if(null != locations && locations.size() > 0){
  					
  					int countFlag = 1;
  					for(CollegeMessage tmp : locations){
  						
  						if(departName.equals(tmp.getDepartment())){
  							CollegeLocation tmpEntity = new CollegeLocation();
  							tmpEntity.setDepartId(departId);
  							tmpEntity.setLocationId(countFlag+"");
  							tmpEntity.setLocationName(tmp.getLocation());
  							location.add(tmpEntity);
  							countFlag++;
  						}
  					}
  				}
  				
  				List<CollegeMessage> schools = collegeMessageMapper.findSchool();
  				
  				if(null != location && location.size() > 0){
  					
  					for(CollegeLocation tmp :location){
  						
  						String locationId = tmp.getLocationId();
  						String locationName = tmp.getLocationName();
  						
  						List<CollegeName> name = new ArrayList<CollegeName>();
  						if(null != schools && schools.size() > 0){
  							for(CollegeMessage school : schools){
  								
  								if(locationName.equals(school.getLocation())){
  									CollegeName cName  = new CollegeName();
  									cName.setLocationId(locationId);
  									cName.setName(school.getCollegeName());
  									name.add(cName);
  								}
  							}
  						}
  						tmp.setCollegeNames(name);
  					}
  				}
  				
  				msg.setCollegeLocations(location);
  			}
  		}
  		
  		return resultList;
  	}

}
